package com.itheima.controller.backend.admin;

import com.itheima.common.dto.CheckItemDTO;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查项管理
 */
@Slf4j
@RestController
//@RequestMapping("/backend/admin/checkitem")
@RequestMapping("/checkitem")
public class CheckitemController {

    @Autowired
    CheckitemService checkitemService;


    /**
     * 新增检查项
     * @return
     */
    @PostMapping("/add")
    public Result insert(@RequestBody CheckItemDTO checkItemDTO){
        log.info("新增检查项");
        checkitemService.insert(checkItemDTO);
        return new Result(true,"新增成功",null);
    }

    /**
     * 删除检查项
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        log.info("删除检查项");
        checkitemService.delete(id);
        return new Result(true,"删除成功",null);
    }

    /**
     * 修改检查项
     * @return
     */
    @PostMapping("/edit")
    public Result update(@RequestBody CheckItemDTO checkItemDTO){
        log.info("修改检查项");
        checkitemService.update(checkItemDTO);
        return new Result(true,"修改成功",null);
    }

    /**
     * 根据检查组id查询关联的检查项id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        log.info("根据检查组{}查询关联的检查项id",id);
        List<Integer> ids = checkitemService.findCheckItemIdsByCheckGroupId(id);

        return new Result(true,"查询检查项成功",ids);
    }

    /**
     * 分页查询
     * @return
     */
    @PostMapping("/findPage")
    public PageResult pages(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询");


        return checkitemService.pages(queryPageBean);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckItem> list = checkitemService.findAll();
        return new Result(true,"查询所有检查项成功",list);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id{}：查询检查项",id);
        CheckItem checkItem = checkitemService.findById(id);
        return new Result(true,"查询检查项成功",checkItem);
    }
}
